# -*- coding: utf-8 -*-
# Import Odoo models and fields
from odoo import models, fields, api, _


class SForecastBomStructureLineWizard(models.TransientModel):
    _name = 's.forecast.bom.structure.line.wizard'
    _description = 'Forecast BOM Structure Line Wizard'

    wizard_id = fields.Many2one('s.forecast.bom.structure.wizard', string='Wizard', required=True, ondelete='cascade')
    product_id = fields.Many2one('product.template', string='Product', required=True)
    name = fields.Char(string='Name', compute='_compute_bom_level_name', store=True)
    product_type = fields.Char(string='Product Category', compute='_compute_bom_product_type', store=True)
    qty = fields.Float(string='Quantity', default=1.0)
    uom_id = fields.Many2one('uom.uom', string='Unit of Measure')
    level = fields.Integer(string='Level', default=1)  # BOM Level field
    default_code = fields.Char(string='Product Code', compute='_compute_product_info', store=True)
    qty_available = fields.Float(string='Quantity On Hand', related='product_id.qty_available', store=True)
    # before_production = fields.Float('Before production', related='product_id.pre_production_qty') # Assuming pre_production_qty exists on product.product
    # free_qty = fields.Float('Free To Use Quantity', related='product_id.free_qty', store=True) # Assuming free_qty exists on product.product
    panel_length_qty = fields.Float(string='Panel Length Qty', compute='_compute_product_info', store=True)
    panel_width_qty = fields.Float(string='Panel Width Qty', compute='_compute_product_info', store=True)
    description = fields.Html(string='Description', related='product_id.description')

    @api.depends('product_id')
    def _compute_bom_product_type(self):
        for item in self:
            category = item.product_id.categ_id
            # Traverse up to find the top-level category
            while category.parent_id:
                category = category.parent_id
            item.product_type = category.name

    @api.depends('product_id', 'level', 'default_code')
    def _compute_bom_level_name(self):
        for item in self:
            level_indent = '——' * max(0, item.level - 1)
            product_name = item.product_id.name if item.product_id else ''
            item.name = f"{level_indent}{item.default_code or ''} {product_name}"

    @api.depends('product_id')
    def _compute_product_info(self):
        for item in self:
            item.default_code = item.product_id.default_code

            # Fetch UOM values for Panel Length and Panel Width
            panel_length_uom = self.env['uom.uom'].search([('name', '=', 'Panel Length')], limit=1)
            panel_width_uom = self.env['uom.uom'].search([('name', '=', 'Panel Width')], limit=1)

            item.panel_length_qty = 0.0
            item.panel_width_qty = 0.0

            if panel_length_uom:
                panel_length_value = self.env['product.uom.value'].search([
                    ('product_id', '=', item.product_id.id),
                    ('uom_id', '=', panel_length_uom.id)
                ], limit=1)
                if panel_length_value:
                    item.panel_length_qty = panel_length_value.uom_value

            if panel_width_uom:
                panel_width_value = self.env['product.uom.value'].search([
                    ('product_id', '=', item.product_id.id),
                    ('uom_id', '=', panel_width_uom.id)
                ], limit=1)
                if panel_width_value:
                    item.panel_width_qty = panel_width_value.uom_value
